package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.dto.RoleDto;
import com.quickcancellation.crm.pojo.dto.RoleMenuDto;
import com.quickcancellation.crm.pojo.entity.Role;
import com.quickcancellation.crm.pojo.vo.MenuVo;
import com.quickcancellation.crm.service.IRoleService;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-25
 */
@CrossOrigin
@RestController
@SaCheckLogin
@Api(tags = "角色管理")
@SaCheckRole("role")
@RequestMapping("/role")
public class RoleController {

    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Resource
    private IRoleService roleService;


    @PostMapping("list")
    @LogAspect()
    @ApiOperation("角色列表")
    public ResponseDto list(@RequestBody PageParamsDto<Role> pageParams) {

        Role roleParam = pageParams.getParams();

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(roleParam.getRoleType())) {
            queryWrapper.like("roleType", roleParam.getRoleType());
        }

        if (StringUtils.hasLength(roleParam.getRolename())) {
            queryWrapper.like("rolename", roleParam.getRolename());

        }

        Page<Role> page = new Page<>(pageParams.getPage(), pageParams.getLimit());

        Page<Role> resultPage = roleService.page(page,queryWrapper);

        return ResponseUtil.success(resultPage.getTotal(), resultPage.getRecords());
    }


    @PostMapping("add")
    @LogAspect()
    @ApiOperation("添加权限")
    @Transactional // 开启事务
    public ResponseDto addRole(@RequestBody RoleDto params) {
//        RoleDto params = pageParams.getParams();


        if (roleService.existRoleByRoleNameOrRoleType(params.getRole().getRolename(),params.getRole().getRoleType())){

            return ResponseUtil.fail("-511","该角色名称或用户类型已经存在");
        }

        boolean result = roleService.save(params.getRole());

        boolean result2 = roleService.saveOrDelRoleMenu(params.getRole().getId(), params.getMenuVoList());

        return (result && result2) ? ResponseUtil.success() : ResponseUtil.fail("-509", "添加失败");

    }

    @PostMapping("delete")
    @LogAspect()
    @ApiOperation("删除角色")
    public ResponseDto deleteRole(@RequestBody Map<String, Object> param) {

        String id = MapUtils.getString(param, "id");

        boolean result = roleService.removeById(id);

        return result ? ResponseUtil.success() : ResponseUtil.fail("-510", "删除失败");

    }

    @PostMapping("getMenuList")
    @LogAspect()
    @ApiOperation("查询用户目录")
    public ResponseDto getUserMenuList(@RequestBody Map<String, Object> params) {
        String roleId = MapUtils.getString(params, "id");

        List<MenuVo> roleVoList = roleService.getMenuByRoleId(roleId);

        return ResponseUtil.success(roleVoList.size(), roleVoList);
    }

    @PostMapping("update")
    @LogAspect()
    @ApiOperation("修改角色")
    public ResponseDto updateRole(@RequestBody RoleMenuDto params) {
        Long roleId = params.getRoleId();

        List<MenuVo> menuVoList = params.getMenuVoList();

        boolean result = roleService.saveOrDelRoleMenu(roleId, menuVoList);

        return result? ResponseUtil.success():ResponseUtil.fail("-511","修改失败");


    }


}
